package com.taobao.dao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import org.apache.http.ParseException;
import org.apache.http.cookie.Cookie;

import com.taobao.pojo.Config;
import com.taobao.pojo.HttpContext;

public abstract class CommonOriginDao {

    private HttpURLConnection getHttpURLConnection(String urlStr) throws Exception {
        int time_out = Integer.parseInt(Config.time_out);
        List<Cookie> cookies = Config.cookieStore.getCookies();
        String session = "";
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName());
            if ("ASP.NET_SessionId".equals(cookie.getName())) {
                session = cookie.getValue();
            }
        }

        URL url = new URL(urlStr);
        HttpURLConnection.setFollowRedirects(true);
        HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
        httpUrlConnection.setConnectTimeout(time_out);
        httpUrlConnection.setReadTimeout(time_out);
        httpUrlConnection.setDoOutput(true);
        httpUrlConnection.setUseCaches(false);
        httpUrlConnection.setRequestProperty("Referer", Config.url + "Tasks.aspx?t=1&jorf=j");
        httpUrlConnection.setRequestProperty("Accept", "*/*");
        httpUrlConnection.setRequestProperty("Accept-Language", "zh-cn");
        httpUrlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
        httpUrlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)");
        httpUrlConnection.setRequestMethod("POST");
        httpUrlConnection.addRequestProperty("Cookie", "ASP.NET_SessionId=" + session);
        httpUrlConnection.connect();
        return httpUrlConnection;
    }

    public Object postRequest(String url, Map<String, String> params) throws Exception {
        HttpURLConnection httpUrlConnection = getHttpURLConnection(url);
        try {
            String data = "";
            for (String key : params.keySet()) {
                data = data + key + "=" + URLEncoder.encode(params.get(key), "UTF-8") + "&";
            }
            data = data.substring(0, data.length() - 1);

            OutputStreamWriter out = new OutputStreamWriter(httpUrlConnection.getOutputStream());
            out.write(data);
            out.flush();
            out.close();
            InputStream is = httpUrlConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            return this.process(getHttpContext(sb.toString()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("CommonOriginDao.postRequest Error " + this.getClass());
        }
    }

    private HttpContext getHttpContext(String html) throws ParseException, IOException {
        try {
            HttpContext context = new HttpContext();
            context.setHtml(html);
            return context;
        } catch (Exception e) {
            throw new ParseException("CommonDao.getHttpContext Error " + this.getClass());
        }
    }

    public abstract Object process(HttpContext context) throws Exception;
}
